<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>绑定邮箱-{eyou:global name='web_name' /}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
    {eyou:static file="users/skin/css/basic.css" /}
    {eyou:static file="users/skin/css/eyoucms.css" /}
    {eyou:include file="users/skin/css/diy_css.htm" /}
    {eyou:static file="/public/static/common/js/jquery.min.js"/}
    {eyou:static file="/public/plugins/layer-v3.1.0/layer.js"/}
    {eyou:static file="/public/static/common/js/tag_global.js"/}
    {eyou:static file="/public/static/common/js/mobile_global.js"/}

    <style type="text/css">
        #theForm .form-group{
            margin:20px 0px 10px;
        }
    </style>
</head>
<body>
<div class="changepass">
    <form name='theForm' id="theForm" method="post">
        <div class="modal-body">
            <div class="form-group">
                <div class="input-group">
                    <input type="text" name="email" id="email" {eyou:eq name='$users.is_email' value='0'} value="{$users.email}" {/eyou:eq} required class="form-control" placeholder="新的邮箱地址">
                </div>
            </div>

            <div class="form-group">
                <div class="input-group yxrz">
                    <input type="text" class="form-control" id="email_code" name="email_code" placeholder="邮箱验证码">
                    <input type="button" id="email_button" onclick="get_email_code();" class="btn btn-primary" value="点击发送" />
                </div>
            </div>

        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-primary" style="width: 100%;" onclick="SubmitData();">确定</button>
        </div>
    </form>
</div>

<script type="text/javascript">

    function get_email_code()
    {
        var email = $("#email").val();
        var reg = /^[a-z0-9]([a-z0-9\\.]*[-_]{0,4}?[a-z0-9-_\\.]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+([\.][\w_-]+){1,5}$/i;
        // 邮箱格式不正确
        if(!reg.test(email)){
            layer.msg('请正确输入邮箱地址！', {time: 2000});
            return false;
        }

        $("#email_button").val('发送中…');
        $("#email_button").attr('disabled', 'disabled');
        var title = '{$title}';

        layer_loading('正在处理');
        $.ajax({
            url: "{eyou:url link='user/Smtpmail/send_email' /}",
            data: {email:email,title:title,type:'bind_email',scene:3},
            type:'post',
            dataType:'json',
            success:function(res){
                if(res.code == 1){
                    countdown();
                    layer.msg(res.msg, {time: 2000});
                }else{
                    $("#email_button").val('点击发送');
                    $("#email_button").removeAttr("disabled");
                    showErrorAlert(res.msg);
                }
            },
            error : function(e) {
                $("#email_button").val('点击发送');
                $("#email_button").removeAttr("disabled");
                showErrorAlert(e.responseText);
            }
        });
    }

    function SubmitData()
    {
        var email = $("#email").val();
        var reg = /^[a-z0-9]([a-z0-9\\.]*[-_]{0,4}?[a-z0-9-_\\.]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+([\.][\w_-]+){1,5}$/i;
        // 邮箱格式不正确
        if(!reg.test(email)){
            layer.msg('请正确输入邮箱地址！', {time: 2000});
            return false;
        }

        var email_code = $("#email_code").val();
        if(!email_code){
            layer.msg('邮箱验证码不能为空！', {time: 2000});
            return false;
        }

        var _parent = parent;
        var parentObj = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

        layer_loading('正在处理');
        $.ajax({
            url: "{eyou:url link='user/Users/bind_email' /}",
            data: $('#theForm').serialize(),
            type:'post',
            dataType:'json',
            success:function(res){
                layer.closeAll();
                if(res.code == 1){
                    parent.layer.close(parentObj);
                    parent.layer.msg(res.msg, {time: 1500}, function(){
                        _parent.window.location.reload();
                    });
                }else{
                    showErrorAlert(res.msg);
                }
            },
            error : function(e) {
                layer.closeAll();
                showErrorAlert(e.responseText);
            }
        });
    };


    function countdown(){
        // 倒计时
        var setTime;
        var time = {php}echo config('global.email_send_time');{/php};
        setTime = setInterval(function(){
            if(0 >= time){
                clearInterval(setTime);
                return;
            }
            time--;
            $("#email_button").val(time+'秒');
            $("#email_button").attr('disabled', 'disabled');

            if(time == 0){
                $("#email_button").val('点击发送');
                $("#email_button").removeAttr("disabled");
            }
        },1000);
    };
</script>

</body>
</html>